package com.ymatou.counter.api.testcase;

import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.counter.api.parameter.SetNaturalMultiBean;
import com.ymatou.counter.api.service.SetNaturalMultiCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 批量设置计数器 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SetNaturalMulti {
	private static SetNaturalMultiBean setnaturalmultibean;
	private static SetNaturalMultiCall setnaturalmulticall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量设置计数器");
	}

	@Before
	public void caseUp() {
		setnaturalmultibean = new SetNaturalMultiBean();
		setnaturalmulticall = new SetNaturalMultiCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setnaturalmulticall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetNaturalMulti_001() {
		Logger.start(true,"批量设置计数器(增加)成功");
		try {
			String TargetId1 = TestDataManager.getRandomUUID().toString();
			String TargetId2 = TestDataManager.getRandomUUID().toString();
			int value1 = new Random().nextInt(5) + 1;
			int value2 = new Random().nextInt(5) + 1;

			Logger.comment("给TargetId1为"+TargetId1 + ":添加计数器 nc1: "+value1);
			new CounterCallService().setNatural("Test", "nc1", TargetId1, 1, value1);
			Logger.comment("给TargetId2为"+TargetId2 + ":添加计数器 nc1: "+value2);
			new CounterCallService().setNatural("Test", "nc1", TargetId2, 1, value2);

			setnaturalmultibean.setAppId("Test");
			String[] TargetIds = { TargetId1, TargetId2 };
			setnaturalmultibean.setTargetIds(TargetIds);
			setnaturalmultibean.setCounter("nc1");
			setnaturalmultibean.setValue(11);
			setnaturalmultibean.setAction(1);
			setnaturalmultibean.setDateTime(YMTDateUtil.getBeforeOrNextDay(0,"yyyy-MM-dd'T'HH:mm:ss"));
			setnaturalmulticall.setData(setnaturalmultibean);
			setnaturalmulticall.callService();
			Logger.verifyEquals(ECode.SUCCESS, setnaturalmulticall.getCode(), "验证CODE状态");
			Logger.verifyEquals(ECode.SUCCESSMSG3, setnaturalmulticall.getMsg(), "验证Msg");
			Logger.verifyEquals(value1 + 11, setnaturalmulticall.getData().get(TargetId1), "验证TargetId1("+TargetId1+")的计数器的值");
			Logger.verifyEquals(value2 + 11, setnaturalmulticall.getData().get(TargetId2), "验证TargetId2("+TargetId2+")的计数器的值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetNaturalMulti_002() {
		Logger.start(true,"批量设置计数器(减少)成功");
		try {
			String TargetId1 = TestDataManager.getRandomUUID().toString();
			String TargetId2 = TestDataManager.getRandomUUID().toString();
			int value1 = new Random().nextInt(5) + 1;
			int value2 = new Random().nextInt(5) + 1;

			Logger.comment("给TargetId1为"+TargetId1 + ":添加计数器 nc1: "+value1);
			new CounterCallService().setNatural("Test", "nc1", TargetId1, 1, value1);
			Logger.comment("给TargetId2为"+TargetId2 + ":添加计数器 nc1: "+value2);
			new CounterCallService().setNatural("Test", "nc1", TargetId2, 1, value2);

			setnaturalmultibean.setAppId("Test");
			String[] TargetIds = { TargetId1, TargetId2 };
			setnaturalmultibean.setTargetIds(TargetIds);
			setnaturalmultibean.setCounter("nc1");
			setnaturalmultibean.setValue(11);
			setnaturalmultibean.setAction(2);
			setnaturalmultibean.setDateTime(YMTDateUtil.getBeforeOrNextDay(0,"yyyy-MM-dd'T'HH:mm:ss"));
			setnaturalmulticall.setData(setnaturalmultibean);
			setnaturalmulticall.callService();
			Logger.verifyEquals(ECode.SUCCESS, setnaturalmulticall.getCode(), "验证CODE状态");
			Logger.verifyEquals(ECode.SUCCESSMSG3, setnaturalmulticall.getMsg(), "验证Msg");
			Logger.verifyEquals(value1 - 11, setnaturalmulticall.getData().get(TargetId1), "验证TargetId1("+TargetId1+")的计数器的值");
			Logger.verifyEquals(value2 - 11, setnaturalmulticall.getData().get(TargetId2), "验证TargetId2("+TargetId2+")的计数器的值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetNaturalMulti_003() {
		Logger.start(true,"批量设置计数器(设置)成功");
		try {
			String TargetId1 = TestDataManager.getRandomUUID().toString();
			String TargetId2 = TestDataManager.getRandomUUID().toString();
			int value1 = new Random().nextInt(5) + 1;
			int value2 = new Random().nextInt(5) + 1;

			Logger.comment("给TargetId1为"+TargetId1 + ":添加计数器 nc1: "+value1);
			new CounterCallService().setNatural("Test", "nc1", TargetId1, 1, value1);
			Logger.comment("给TargetId2为"+TargetId2 + ":添加计数器 nc1: "+value2);
			new CounterCallService().setNatural("Test", "nc1", TargetId2, 1, value2);

			setnaturalmultibean.setAppId("Test");
			String[] TargetIds = { TargetId1, TargetId2 };
			setnaturalmultibean.setTargetIds(TargetIds);
			setnaturalmultibean.setCounter("nc1");
			setnaturalmultibean.setValue(11);
			setnaturalmultibean.setAction(3);
			setnaturalmultibean.setDateTime(YMTDateUtil.getBeforeOrNextDay(0,"yyyy-MM-dd'T'HH:mm:ss"));
			setnaturalmulticall.setData(setnaturalmultibean);
			setnaturalmulticall.callService();
			Logger.verifyEquals(ECode.SUCCESS, setnaturalmulticall.getCode(), "验证CODE状态");
			Logger.verifyEquals(ECode.SUCCESSMSG3, setnaturalmulticall.getMsg(), "验证Msg");
			Logger.verifyEquals(11, setnaturalmulticall.getData().get(TargetId1), "验证TargetId1("+TargetId1+")的计数器的值");
			Logger.verifyEquals(11, setnaturalmulticall.getData().get(TargetId2), "验证TargetId2("+TargetId2+")的计数器的值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetNaturalMulti_004() {
		Logger.start(true,"批量设置计数器(清零)成功");
		try {
			String TargetId1 = TestDataManager.getRandomUUID().toString();
			String TargetId2 = TestDataManager.getRandomUUID().toString();
			int value1 = new Random().nextInt(5) + 1;
			int value2 = new Random().nextInt(5) + 1;

			Logger.comment("给TargetId1为"+TargetId1 + ":添加计数器 nc1: "+value1);
			new CounterCallService().setNatural("Test", "nc1", TargetId1, 1, value1);
			Logger.comment("给TargetId2为"+TargetId2 + ":添加计数器 nc1: "+value2);
			new CounterCallService().setNatural("Test", "nc1", TargetId2, 1, value2);

			setnaturalmultibean.setAppId("Test");
			String[] TargetIds = { TargetId1, TargetId2 };
			setnaturalmultibean.setTargetIds(TargetIds);
			setnaturalmultibean.setCounter("nc1");
			setnaturalmultibean.setValue(11);
			setnaturalmultibean.setAction(4);
			setnaturalmultibean.setDateTime(YMTDateUtil.getBeforeOrNextDay(0,"yyyy-MM-dd'T'HH:mm:ss"));
			setnaturalmulticall.setData(setnaturalmultibean);
			setnaturalmulticall.callService();
			Logger.verifyEquals(ECode.SUCCESS, setnaturalmulticall.getCode(), "验证CODE状态");
			Logger.verifyEquals(ECode.SUCCESSMSG3, setnaturalmulticall.getMsg(), "验证Msg");
			Logger.verifyEquals(0, setnaturalmulticall.getData().get(TargetId1), "验证TargetId1("+TargetId1+")的计数器的值");
			Logger.verifyEquals(0, setnaturalmulticall.getData().get(TargetId2), "验证TargetId2("+TargetId2+")的计数器的值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}